package com.sky.web.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "分类管理")
// 分类管理
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    // 分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public Result findByPage(CategoryPageQueryDTO dto) {
        PageBean<Category> pageBean = categoryService.findByPage(dto);
        return Result.success(pageBean);
    }

    // 新增分类
    @ApiOperation(value = "新增分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "分类对象", required = true, dataType = "Category")
    })
    @PostMapping
    public Result save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success();
    }

    // 删除分类
    @ApiOperation(value = "删除分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类id", required = true, dataType = "Long")
    })
    @DeleteMapping
    public Result deleteById(Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    // 作业--根据id修改分类
    @ApiOperation(value = "根据id修改分类")
    @PutMapping
    public Result updateById(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }

    // 作业--分类启用禁用
    @ApiOperation(value = "分类启用禁用")
    @PostMapping("/status/{status}")
    public Result startOrStop(Long id, @PathVariable("status") Integer status) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        categoryService.update(category);
        return Result.success();
    }

    // 根据type查询分类列表
    @ApiOperation(value = "根据type查询分类列表")
    @GetMapping("/list")
    public Result findByType(Integer type) {
        List<Category> list = categoryService.findByType(type);
        return Result.success(list);
    }
}
